<?php
//
//	file: includes/calendar/birthday/class_calendar_birthday_dmy_sep.php
//	author: ptirhiik
//	begin: 24/04/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

// --------------
//
// birthday format is varchar(10), dd-mm-yyyy, no leading 0 for each component (eg: 1-2-1970)
//	not filled can be NULL, '' and '--'
//
// --------------

class calendar_birthday_layer
{
	function read($user_fields, $xfrom, $xto)
	{
		global $db, $user, $config;

		$data = array();

		$tac = $xto['y'] < 0 ? -1 : +1;
		$fac = $xfrom['y'] < 0 ? -1 : +1;
		$tm = $tac * ($xto['m'] - 1);
		$fm = $fac * ($xfrom['m'] - 1);
		$td = $tac * ($xto['d'] - 1);
		$fd = $fac * ($xfrom['d'] - 1);
		$delta = ($xto['y'] - $xfrom['y']) * 372 + ($tm - $fm) * 31 + ($td - $fd) - 1;
		if ( $delta < 0 )
		{
			return array();
		}
		$select = $delta < 372; // 31 * 12

		// check if we need the twelves monthes
		if ( $select )
		{
			$mfrom = $xfrom['m'] - 1;
			$mto = ($xto['y'] - $xfrom['y']) * 12 + $tm + ($xto['d'] > 1 ? 1 : 0);
			$monthes = array();
			for ( $m = $mfrom; $m < $mto; $m++ )
			{
				$monthes[ (($m % 12) + 1) ] = true;
			}
			$select = count($monthes) < 12;
		}

		// no selection : get all birthday
		if ( !$select )
		{
			$sql = 'SELECT ' . implode(', ', $this->user_fields) . '
						FROM ' . USERS_TABLE . '
						WHERE user_id <> ' . ANONYMOUS . '
							AND user_active = 1
							AND user_birthday IS NOT NULL
							AND user_birthday <> \'\'
							AND user_birthday <> \'--\'
						ORDER BY username';
		}

		// select
		else
		{
			$sql_where = array();
			if ( ($sql_or = count($monthes) <= 6) )
			{
				foreach ( $monthes as $i => $dummy )
				{
					$sql_where[] = 'user_birthday LIKE \'%-' . $i . '-%\'';
				}
			}
			else
			{
				for ( $i = 1; $i <= 12; $i++ )
				{
					if ( !$monthes[$i] )
					{
						$sql_where[] = 'user_birthday NOT LIKE \'%-' . $i . '-%\'';
					}
				}
			}

			// build the sql request
			$sql = 'SELECT ' . implode(', ', $this->user_fields) . '
						FROM ' . USERS_TABLE . '
						WHERE user_id <> ' . ANONYMOUS . '
							AND user_active = 1
							AND user_birthday IS NOT NULL
							AND user_birthday <> \'\'
							AND user_birthday <> \'--\'
							AND (
								' . implode('
								' . ($sql_or ? 'OR ' : 'AND '), $sql_where) . '
							)
						ORDER BY username';
		}
		if ( !($result = $db->sql_query($sql, false, __LINE__, __FILE__)) )
		{
			message_die(GENERAL_ERROR, 'Could not read users data', '', __LINE__, __FILE__, $sql);
		}
		while ( $row = $db->sql_fetchrow($result) )
		{
			$b = explode('-', $row['user_birthday']);
			$row['user_birthday'] = array('m' => intval($b[1]), 'd' => intval($b[0]), 'y' => intval($b[2]));
			if ( !$config->globals['mod_topic_calendar_CH']->api->is_gt($xfrom, $row['user_birthday']) && $config->globals['mod_topic_calendar_CH']->api->is_gt($xto, $row['user_birthday']) )
			{
				$data[ intval($row['user_id']) ] = $row;
			}
		}
		$db->sql_freeresult($result);
		return $data;
	}
}

?>